
package ija.model.figures;

import java.util.List;

import ija.model.basis.Position;
import ija.model.basis.Figure;
import ija.model.types.Color;
import ija.model.types.Direction;

/**
 * Třída reprezentuje běžný kámen ve hře dáma
 * 
 * @author Radek Necas, xnecas18
 */
public class Piece extends CheckersFigure {

    /** Směr pohybu pěšce */
    Direction direction;
    
    /**
     * 
     * @param p pozice, kam bude kámen umístěn.
     * @param c barva kamene.
     * @param d směr hry kamene (dopředu znamená pohyb z nižšího řádku na vyšší).
     */
    public Piece(Position p, Color c, Direction d) {
        super(p, c);
        direction = d;
    }
    
  public String tryMoreJumps(List<String> list, String moveNotation){
  	  
	  //Jump();
  	  boolean jumpedAgain = false;
	  int[] q = {-1,1};
  	  for (int x: q){
  	      for (int y: q){
  		      Position newJumpPos = this.position.nextPosition(2*x, 2*y);
  		      if ((newJumpPos != null) && (this.canJump(newJumpPos)!=null)){
  		    	moveNotation += 'x'+newJumpPos.toString();
  			     tryMoreJumps(list, moveNotation);
  			     jumpedAgain = true;
  		      }  
  	      }
      }
  	  if (!jumpedAgain) {list.add(moveNotation);};
  	  return moveNotation;
  } 
    
    
    public boolean getMoves(List<String> list, boolean jumpsonly){
    	int[] q = {-1,1};
    	String moveNotation = new String();
    	  for (int x: q){
    	      for (int y: q){
    	    	  Position newJumpPos = this.position.nextPosition(2*x, 2*y);
    	    	  Position newMovePos = this.position.nextPosition(x, y);
    	    	  if ((newJumpPos != null) && (this.canJump(newJumpPos)!=null)){
    	    		  if (!jumpsonly) {
    	    			  list.clear(); 
    	    			  jumpsonly = true;
    	    		  }
    	    		  moveNotation = this.position.toString()+'x'+newJumpPos.toString();
    	    		  
    	    		  Position prevPos = this.position;
    	    		  Position killedPos = newMovePos;
    	    		  Figure killedFig = killedPos.getFigure();
    	    		  
    	    		  this.jump(newJumpPos);
    	    		  
    	    		  tryMoreJumps(list,moveNotation);
    	    		  
    	    		  killedPos.putFigure(killedFig);
    	    		  killedFig.uncheckedMove(killedPos);
    	    		  this.uncheckedMove(prevPos);
    	    	  }
    	    	  
    	    	  if ((!jumpsonly) && (this.canMove(newMovePos))){
    	    		  if (newMovePos.getFigure() == null) {
    	    			  list.add(moveNotation);
    	    		  }
    	    	  }
          }
      }
      return jumpsonly;
    }
        
    /**
     * Nastavení směru kamene
     * @param d směr pohybu na který se má kámen nastavit.
     */
    public void setDirection(Direction d) {
        direction = d;
    }
    
    /**
     * Metoda pro získání směru kamene
     * @return směr jakým se kamen pohybuje
     */
    public Direction getDirection() {
        return direction;
    }

    @Override
    public Position canJump(Position endPosition) {
        if(!position.sameDiagonal(endPosition) || endPosition.getFigure() != null)
            return null;
        
        int dR = endPosition.getRowNumber() - position.getRowNumber();
        int dC = endPosition.getColumnChar() - position.getColumnChar();
        int expectedDR = (direction == Direction.FORWARD ? 2 : -2);
        Position p;
        if(dR == expectedDR && (dC == 2 || dC == -2)) {
            p = position.nextPosition(dC/2, dR/2);            
            if(p.getFigure().getColor() != color)
                return(p);
        }
        
        return null;
    }
    
    public boolean inDirection(Position p){
        if(position.sameDiagonal(p)) {
            int dp = (direction == Direction.FORWARD ? 1 : -1);
                     
            if((p.getRowNumber() - position.getRowNumber()) == dp) {         // Bezny posun o jednu pozici
                return true;
            }
        }    
        return false;	
    }
    
    @Override
    public boolean canMove(Position p) {
        if(p.getFigure() != null)
            return false;
        if (!(p.getDesk().isInside(p)))
        	return false;
        return inDirection(p);
    }
}
